<template>
  <el-dialog
    v-model="visible"
    v-if="load"
    title="批量审核"
    width="560"
    draggable
    @close="close"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :z-index="2000"
  >
    <div class="mb-4">
      <div class="mb-4 text-sm font-bold">
        选中单据：<span class="text-blue-500 font-bold text-base">{{
          form.skuIds.length
        }}</span>
      </div>
    </div>
    <div class="mb-4">
      <div class="mb-4 text-sm font-bold">审核状态</div>
      <el-radio-group v-model="form.reviewOpinion">
        <el-radio
          :label="item.value"
          :value="item.value"
          v-for="(item, index) in reviewOptions"
          :key="index"
          >{{ item.label }}</el-radio
        >
      </el-radio-group>
    </div>
    <div class="mb-4">
      <div class="mb-4 text-sm font-bold">审核备注</div>
      <el-input
        v-model="form.reviewRemark"
        type="textarea"
        placeholder="请输入"
        :autosize="{ minRows: 4, maxRows: 8 }"
      ></el-input>
    </div>
    <template #footer>
      <el-button @click="close">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save()">保存</el-button>
    </template>
  </el-dialog>
</template>
<script setup name="BatchAudit">
import { reactive, toRefs } from "vue";
import { skuReview } from "../../api/index";
import { ElMessage } from "element-plus";

const emits = defineEmits(["query"]);

const data = reactive({
  visible: false,
  loading: false,
  form: {},
  load: false,
});
const { visible, loading, form, load } = toRefs(data);

const reviewOptions = [
  {
    label: "审核通过",
    value: 1,
  },
  {
    label: "审核未通过",
    value: 0,
  },
];

const open = async (data) => {
  if (!load.value) {
    load.value = true;
    await 1;
  }
  form.value.skuIds = data;
  visible.value = true;
};
const close = () => {
  visible.value = false;
};

// let formTips = new Map([
//   ["reviewOpinion", "请选择审核状态"],
//   ["reviewRemark", "请填写审核备注"],
// ]);

const save = async () => {
  // let keys = Object.keys(form.value);
  // let key_values = Object.values(form.value);
  // if (key_values.some((i) => !i || i.length == 0)) {
  //   let index = key_values.findIndex((i) => !i);
  //   let key = keys[index];
  //   let tips = formTips.get(key);
  //   ElMessage.error(tips)
  //   if (tips) return false;
  // }
  try {
    const res = await skuReview(form.value);
    ElMessage.success(res.message);
    initForm();
    emits("query");
    close();
  } catch (e) {
    throw Error(e);
  }
};
// 初始化
const getForm = () => {
  return {
    reviewType: 3,
    reviewOpinion: "",
    reviewRemark: "",
    skuIds: [],
  };
};
const initForm = () => {
  form.value = getForm();
};

onMounted(() => {
  initForm();
});

defineExpose({ open });
</script>
